﻿using System;
using System.Collections.Generic;

using System.Text;
using NHibernate;
using NHibernate.Criterion;
using OpenGado.DATA.Entities;

namespace OpenGado.DATA
{
    public class ContactsDataControl
    {

        private static ContactsDataControl contactsDataControl;
        private static object lockContacts = new object();

        private ISession session;
        /// <summary>
        /// Handles sessions
        /// </summary>
        public ISession Session
        {

            get
            {
                    session = FactoryHandler.CreateFactory().OpenSession();

               
                return session;
            }


        }
        /// <summary>
        /// Handles instances
        /// </summary>
        public static ContactsDataControl Instance
        {

            get
            {
                lock (lockContacts)
                {
                    if (contactsDataControl ==null)
                    {
                        contactsDataControl = new ContactsDataControl();
                    }
                   
                }
                return contactsDataControl;
            }
        }

        /// <summary>
        /// Saves contact data
        /// </summary>
        /// <param name="contact"></param>
        public void Save (Contact contact)
        {

            ITransaction transaction = null;
            ISession tsession = Session;
            try
            {
                transaction = tsession.BeginTransaction();
                tsession.SaveOrUpdate(contact);
                transaction.Commit();
                
            }
            catch (Exception)
            {
                if (transaction != null) transaction.Rollback();
                throw;
            }
        }
        /// <summary>
        /// Deletes contact data
        /// </summary>
        /// <param name="contact"></param>
        public void Delete (Contact contact)
        {

            ITransaction transaction = null;
            ISession tsession = Session;

            try
            {
                transaction = tsession.BeginTransaction();
                tsession.Delete(contact);
                transaction.Commit();
            }
            catch (Exception)
            {
                if (transaction != null) transaction.Rollback();
                throw;
            }
        }
        /// <summary>
        /// Returns contact item 
        /// </summary>
        /// <param name="contactId"></param>
        /// <returns></returns>
        public Contact GetContactById(int contactId)
        {

          return  this.Session.Get<Contact>(contactId);
        }
        /// <summary>
        /// Returns all contacts in Db
        /// </summary>
        /// <returns></returns>
        public IList<Contact> GetContactList()
        {
            ISession tsession = Session;
            ICriteria criteria = tsession.CreateCriteria<Contact>();
            IList<Contact> contacts = criteria.List<Contact>();
            tsession.Close();


            return contacts;
        }
        /// <summary>
        /// Saves Address
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        public void SaveAddress(Address address)
        {
            ITransaction transaction = null;

            try
            {
                transaction = Session.BeginTransaction();
                this.Session.SaveOrUpdate(address);
                transaction.Commit();
                
            }
            catch (Exception)
            {
                if (transaction != null) transaction.Rollback();
                throw;
            }

        }
        /// <summary>
        /// Deletes address
        /// </summary>
        /// <param name="address"></param>
        public void DeleteAddress(Address address)
        {

            ITransaction transaction = null;

            try
            {
                transaction = Session.BeginTransaction();
                this.Session.Delete(address);
                transaction.Commit();
            }
            catch (Exception)
            {
                

                throw;
            }

        }
        /// <summary>
        /// Returns a instances of address by its Id
        /// </summary>
        /// <param name="addressId"></param>
        /// <returns></returns>
        public Address GetAddressById(int addressId)
        {
            return this.Session.Get<Address>(addressId);
        }
        /// <summary>
        /// Return the list of addresses for a particular contact
        /// </summary>
        /// <returns></returns>
        public IList<Address> GetAllAddresses (Contact contact)
        {
            ICriteria criteria = Session.CreateCriteria<Address>();
            criteria.Add(Expression.Eq("contactID", contact.Contactid));
            IList<Address> addresses = criteria.List<Address>();

            return addresses;
        }

        /// <summary>
        /// Deletes a phone number
        /// </summary>
        /// <param name="phone"></param>
        public void DeletePhone (Phone phone)
        {
            this.Session.Delete(phone);
        }
        /// <summary>
        /// Saves a phone number
        /// </summary>
        /// <param name="phone"></param>
        public void SavePhone(Phone phone)
        {
            ITransaction transaction = null;
            try
            {
                transaction = Session.BeginTransaction();

                this.Session.SaveOrUpdate(phone);

                transaction.Commit();
            }
            catch (Exception)
            {
                if (transaction != null) transaction.Rollback();
                throw;
            }
        }



    }
}
